package com.ricohgag.controller.admin;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricohgag.entity.NewsCatalog;
import com.ricohgag.service.NewsCatalogService;
import com.ricohgag.util.vo.NewsCatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Controller
@RequestMapping("/admin/newsCatalog")
public class NewsCatalogController {
    @Autowired
    private NewsCatalogService newsCatalogService;

    @RequestMapping("/index")
    public ModelAndView toIndex(@RequestParam(value = "pageNow", required = false) Integer current, HttpServletRequest request) throws Exception{
        ModelAndView mav = new ModelAndView("/admin/newsCatalog/index");

        String url = request.getContextPath()+"/admin/newsCatalog/index";
        if(current == null){
            current = 1;
        }

        Page<NewsCatalog> page = new Page<>(current, 10);
        IPage<NewsCatalog> goodsIPage = newsCatalogService.selectNewsCatalog(page);

        mav.addObject("Pager", goodsIPage);
        mav.addObject("pages", goodsIPage.getPages());
        mav.addObject("url",url);
        return mav;

    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(Integer id) throws Exception{
        ModelAndView mav = new ModelAndView("admin/newsCatalog/saveOrUpdate");
        if(!"0".equals(id)){
            NewsCatalog newsCatalog = newsCatalogService.selectNewsCatalogOne(id);
            mav.addObject("newsCatalog", newsCatalog);
        }
        List<NewsCatalogVo> newsCatalogVos = newsCatalogService.selectNewsCatalogVo();
        mav.addObject("newsCatalogVos", newsCatalogVos);

        return mav;
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(NewsCatalog newsCatalog, MultipartFile imgFile, HttpServletRequest request) throws Exception {
        if(newsCatalog.getId() == null){
            newsCatalogService.insertNewsCatalog(newsCatalog);
        } else {
            newsCatalogService.updateNewsCatalog(newsCatalog);
        }

        return "/common/public/reload";
    }

    /**
     * 新闻分类跟其他不一样，考虑子类一起删除
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteNewsCatalog(Integer id, HttpServletResponse response) throws Exception{
        Map<String, String> map = new HashMap<>();
        int count = newsCatalogService.deleteNewsCatalog(id);
        System.out.println("delete: "+count);
        if(count > 0){
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }

        String jsonStr = JSON.toJSONString(map);
        return jsonStr;
    }


}

